# -*- coding: utf-8 -*- 
# 作者:飞虎
# 开发时间:2022/5/12 20:51
from PyQt5.Qt import *


class Window(QWidget):
	def __init__(self):
		super().__init__()
		self.setWindowTitle('QRubberBand学习')
		self.resize(500, 500)
		self.setup_ui()

	def setup_ui(self):
		# 0. 添加子控件
		for i in range(0, 30):
			cb = QCheckBox(self)
			# cb.setText(str(i))
			cb.setText("{}".format(i))
			cb.move(20+i % 4 * 50, 20+i // 4 * 60)

	def mousePressEvent(self, evt):
		QMouseEvent
		# 1. 创建一个橡皮筋选中
		self.rb = QRubberBand(QRubberBand.Rectangle, self)
		# 2. 尺寸大小,鼠标点击的位置,00
		self.origin_pos = evt.pos()
		self.rb.setGeometry(QRect(self.origin_pos, QSize()))
		# 3.展示橡皮筋控件
		self.rb.show()


	def mouseMoveEvent(self, evt):
		self.rb.setGeometry(QRect(self.origin_pos, evt.pos()).normalized())

	def mouseReleaseEvent(self, evt):
		rect =self.rb.geometry()
		for child in self.children() :
			if rect.contains(child.geometry()) and child.inherits("QCheckBox"):
				# QCheckBox
				child.toggle()

		self.rb.hide()

if __name__ == '__main__':
	import sys

	app = QApplication(sys.argv)
	window = Window()
	window.show()
	sys.exit(app.exec_())
